import { defineStore } from "pinia"
import { IdLabel, IdName } from '@/types/global'
import { useUserStore } from "./user"
import { getSubcontractorList, getPersonTypeList, getPersonCategoryList } from '@/api/global'

export const useGlobalStore = defineStore({
  id: 'global',
  state() {
    return {
      subcontractorList: [] as IdName[],
      personTypeList: [] as IdName[],
      personCategoryList: [] as IdName[]
    };
  },
  actions: {
    getSubcontractorList() {
      return new Promise((resolve, reject) => {
        getSubcontractorList()
          .then(res => {
            const data = res.data 
            if(res.code === 200) {
              this.subcontractorList = data as IdName[]
            }
            resolve(data)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
    getPersonTypeList() {
      return new Promise((resolve, reject) => {
        getPersonTypeList()
          .then(res => {
            const data = res.data 
            if(res.code === 200) {
              this.personTypeList = data as IdName[]
            }
            resolve(data)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
    getPersonCategoryList() {
      return new Promise((resolve, reject) => {
        getPersonCategoryList()
          .then(res => {
            const data = res.data 
            if(res.code === 200) {
              this.personCategoryList = data as IdName[]
            }
            resolve(data)
          })
          .catch(err => {
            reject(err)
          })
      })
    }
  },
  getters: {
    communityList(): IdLabel[] {
      const { userInfo: { community_arr } } = useUserStore()
      return community_arr
    },
    communityALlChild() {
      const { userInfo: { community_child_arr } } = useUserStore()
      return community_child_arr
    }
  }
})

